<template>
  <el-card>
    <div class="date">
      <div v-for="(i, q) in list" :key="q" style="margin:20px">
        <!-- 月份 -->
        <div style="text-align:center;font-weight: bolder;">
          {{ q + 1 }} 月 {{ year | setYear }} 年
        </div>
        <div
          style="width:100%;display:flex;justify-content:space-around;padding:10px 0;color:#a0aec0;"
        >
          <span>一</span>
          <span>二</span>
          <span>三</span>
          <span>四</span>
          <span>五</span>
          <span>六</span>
          <span>七</span>
        </div>
        <table>
          <tr v-for="(j, w) in i" :key="w">
            <td v-for="v in j[0]" :key="v.id">
              <div
                @click="setDate(v)"
                class="dayBox"
                :style="{ border: v.date ? '1px solid #ccc' : 'none' }"
              >
                {{ v.date | formatDate }}
                <div class="name" v-if="v.name">
                  {{ v.name }}
                </div>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "workTime",
  props: {
    date: {
      type: Array,
      default() {
        return [];
      }
    },
    year: {
      type: Date,
      default() {
        return new Date();
      }
    }
  },
  data() {
    return {
      list: {}
    };
  },
  created() {
    this.initDate();
  },
  filters: {
    formatDate(val) {
      if (val) {
        return val.split(" ")[0].split("-")[2];
      }
    },
    setYear(val) {
      return val.getFullYear();
    }
  },
  methods: {
    initDate() {
      // 将日期数组转成三维数字，顶层为月份，二层为每个月周的数量（向上取整），三层为每周里的每天。获取每周日的第一天，并根据是周几，来填充周几 - 1 的项。
      // 划分月份
      let result = [];
      for (let i = 0; i < 12; i++) {
        result[i] = [];
      }
      // 按月份
      this.date.map(i => {
        let month = i.date.split("-")[1];
        switch (month) {
          case "01":
            result[0].push(i);
            break;
          case "02":
            result[1].push(i);
            break;
          case "03":
            result[2].push(i);
            break;
          case "04":
            result[3].push(i);
            break;
          case "05":
            result[4].push(i);
            break;
          case "06":
            result[5].push(i);
            break;
          case "07":
            result[6].push(i);
            break;
          case "08":
            result[7].push(i);
            break;
          case "09":
            result[8].push(i);
            break;
          case "10":
            result[9].push(i);
            break;
          case "11":
            result[10].push(i);
            break;
          case "12":
            result[11].push(i);
            break;
        }
      });
      let list = [];
      result.map((i, k) => {
        let element = i; // 每个月份数组
        let { date } = element[0];
        let week = new Date(date).getDay();
        if (week == 0) {
          week = 7;
        }
        // 填充长度
        for (let i = 0; i < week - 1; i++) {
          element.unshift({});
        }
        let viv = Math.ceil(element.length / 7);
        let tem = [];
        for (let j = 0; j < viv; j++) {
          tem[j] = [];
          tem[j].push(element.slice(j * 7, (j + 1) * 7));
        }
        list[k] = tem;
      });
      this.list = list;
    },
    setDate({ date }) {
      this.$confirm("确定进行假休班转换吗?", "假休班转换").then(() => {
        this.API.updateWorkday({ date }).then(ret => {
          this.success("操作成功！");
          this.$emit("update");
        });
      });
    }
  }
};
</script>

<style scoped>
.date {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
table {
  width: 100%;
  border-collapse: collapse;
}
table td {
  padding: 2px;
  width: 45px;
  height: 45px;
  box-sizing: border-box;
}
.dayBox {
  padding: 1rem;
  position: relative;
  cursor: pointer;
}
.dayBox:hover {
  background-color: rgb(241, 241, 241);
}
.name {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 12px;
  color: crimson;
}
</style>
